<?php
namespace module\tv\controller;

use think\Request;
use think\Config;
use core\tv\model\VideoSourceModel;
use core\tv\logic\VideoSourceLogic;
use core\tv\validate\VideoSourceValidate;
use app\common\App;

class VideoSource extends Base
{

    /**
     * 视频资源列表
     * 
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $this->siteTitle = '资源管理';
        
        $map = [];

        //查询条件-ID
        $id = $request->param('id', '');
        if ($id != '') {
            $id = intval($id);
            $map['vs.video_id'] = $id;
        }

        // 查询条件-状态
        $status = $request->param('status', '');
        if ($status != '') {
            $status = intval($status);
            $map['vs.status'] = $status;
        }
        $this->assign('status', $status);

        // 查询条件-关键词
        $keyword = $request->param('keyword');
        if ($keyword != '') {
            $map['vs.chapter|vs.video_id|v.video_name'] = [
                'like',
                '%' . $keyword . '%'
            ];
        }
        $this->assign('keyword', $keyword);

        //分页列表
        $model = VideoSourceModel::getInstance();
        $model->withVideo()
        ->field('vs.*,v.video_name')
        ->where($map)
        ->order('vs.id DESC, vs.play_sort DESC');
        $this->_page($model);

        //资源状态选择
        $sourceStatus = VideoSourceLogic::getInstance()->getSourceStatus();
        $this->assign('sourceStatus', $sourceStatus);

        return $this->fetch();
    }

    /**
     * 增加视频资源
     * 
     * @param Request $request
     * @return mixed
     */
    public function add(Request $request)
    {
        if($request->isPost()){
            $data = [
                'video_id' => $request->param('video_id'),
                'chapter' => $request->param('chapter'),
                'cover' => $request->param('cover'),
                'source_src' => $request->param('source_src'),
                'play_sort' => $request->param('play_sort', 0),
                'status' => $request->param('status'),
            ];

            //验证
            $this->_validate(VideoSourceValidate::class, $data, 'add');

            //转码
            $this->transcode($data);

            //添加
            $this->_add(VideoSourceModel::class, $data);
        }else {
            //视频列表选择
            $videoList = VideoSourceLogic::getInstance()->getVideoSelect();
            $this->assign('videoList', $videoList);

            //状态选择
            $sourceStatus = VideoSourceLogic::getInstance()->getSourceStatus();
            $this->assign('sourceStatus', $sourceStatus);

            //视频ID
            $id = $request->param('id','');
            $this->assign('_id', $id);

            return $this->fetch();
        }
    }

    /**
     * 编辑视频资源
     * 
     * @param Request $request
     * @return mixed
     */
    public function edit(Request $request)
    {
        if($request->isPost()){
            $data = [
                'video_id' => $request->param('video_id'),
                'chapter' => $request->param('chapter'),
                'cover' => $request->param('cover'),
                'source_src' => $request->param('source_src'),
                'play_sort' => $request->param('play_sort', 0),
                'status' => $request->param('status'),
            ];

            //验证
            $this->_validate(VideoSourceValidate::class, $data, 'edit');

            //转码
            $id = $request->param('id');
            $source = VideoSourceModel::getInstance()->find($id);
            if($source && $source->source_src != $data['source_src']){
                $this->transcode($data);
            }

            //修改
            $this->_edit(VideoSourceModel::class, $data);
        }else {
            $this->siteTitle = '编辑资源';

            //视频列表选择
            $videoList = VideoSourceLogic::getInstance()->getVideoSelect();
            $this->assign('videoList', $videoList);

            //状态选择
            $sourceStatus = VideoSourceLogic::getInstance()->getSourceStatus();
            $this->assign('sourceStatus', $sourceStatus);

            //记录
            $this->_record(VideoSourceModel::class);

            return $this->fetch();
        }
    }

    /**
     * 修改字段值
     *
     * @param Request $request            
     * @return mixed
     */
    public function modify(Request $request)
    {
        $fields = [
            'play_sort',
            'status'
        ];
        $this->_modify(VideoSourceModel::class, $fields);
    }

    /**
     * 删除视频资源
     * 
     * @param Request $request
     * @return void
     */
    public function delete(Request $request)
    {
        $this->_delete(VideoSourceModel::class, false);
    }

    /**
     * 异步视频转码
     * 
     * @param 
     * @return 
     */
    public function transcode($data = [])
    {
        return true;
        
        //本地上传驱动
        if(Config::get('upload_driver') == 'local'){
            //return true;
        }

        //又拍上传驱动
        if(Config::get('upload_driver') == 'upyun'){
            $upload = App::getSingleton()->upload;
            $Upyun = $upload->getStorage()->upyun();
            dump($Upyun->process());
        }

        
    }
    
}